{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d6faa54d-7a75-40c9-8cbc-925d6fa6e901",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import statsmodels.api as sm\n",
    "from linearmodels.panel import PanelOLS\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "from scipy.stats import ttest_ind"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11d55414-d504-4892-b0a0-c98cd2181766",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_excel(r\"E:\\df_figure.xlsx\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "481d5774-18f1-4093-84c8-e65ec04fa9de",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Rename columns for consistency\n",
    "df.rename(columns={\n",
    "    'Year': 'year',\n",
    "    'Birth rate': 'birth_rate',\n",
    "    'Foreigners': 'foreigners',\n",
    "    'Population growth': 'population_growth'\n",
    "}, inplace=True)\n",
    "\n",
    "# Group by year (if needed) to get yearly averages\n",
    "df_yearly_avg = df.groupby(\"year\")[[\"birth_rate\", \"foreigners\", \"population_growth\"]].mean().reset_index()\n",
    "\n",
    "# Set up subplots\n",
    "fig, axs = plt.subplots(3, 1, figsize=(14, 13), sharex=True, constrained_layout=True)\n",
    "\n",
    "# Plot styling\n",
    "colors = ['darkred', 'navy', 'seagreen']\n",
    "markers = ['o', 's', '^']\n",
    "titles = [\n",
    "    \"(a) Yearly Birth Rate\",\n",
    "    \"(b) Yearly Foreigner Population\",\n",
    "    \"(c) Yearly Population Growth\"\n",
    "]\n",
    "ylabels = [\n",
    "    \"Birth Rate\",\n",
    "    \"Foreigner Population\",\n",
    "    \"Population Growth\"\n",
    "]\n",
    "columns = [\"birth_rate\", \"foreigners\", \"population_growth\"]\n",
    "\n",
    "# Plot each variable with trend line\n",
    "for i, column in enumerate(columns):\n",
    "    axs[i].plot(df_yearly_avg[\"year\"], df_yearly_avg[column], marker=markers[i],\n",
    "                linestyle='dotted', color=colors[i], label=f\"Yearly {column}\", linewidth=1.5)\n",
    "\n",
    "    # Linear trend line\n",
    "    z = np.polyfit(df_yearly_avg[\"year\"], df_yearly_avg[column], 1)\n",
    "    p = np.poly1d(z)\n",
    "    axs[i].plot(df_yearly_avg[\"year\"], p(df_yearly_avg[\"year\"]), color=colors[i],\n",
    "                linewidth=3, label=f\"Trend {column}\")\n",
    "\n",
    "    axs[i].set_ylabel(ylabels[i], fontsize=16)\n",
    "    axs[i].set_title(titles[i], fontsize=18, pad=17)\n",
    "    axs[i].tick_params(axis='both', labelsize=16)\n",
    "    axs[i].legend(loc='upper left', fontsize=15)\n",
    "    axs[i].grid(True)\n",
    "\n",
    "#plt.xlabel(\"Year\", fontsize=16)\n",
    "plt.savefig(\"Figure_1.jpg\", dpi=600)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78ee1a67-1ade-4c3b-a2c8-82c533ded184",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ef0d566e-9a98-40c3-8c85-f8da1feea4cd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
